{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数中参数的传递"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 缺省参数的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "你作业做的太棒了, 萧敬腾!\n",
      "我第一次做的时候绝对做不了这么好.\n",
      "\n",
      "你作业做的太棒了, 张学友!\n",
      "我第一次做的时候绝对做不了这么好.\n",
      "\n",
      "你作业做的太棒了, 刘德华!\n",
      "我第一次做的时候绝对做不了这么好.\n"
     ]
    }
   ],
   "source": [
    "def thank_you(name):\n",
    "    # This function prints a two-line personalized thank you message.\n",
    "    print(\"\\n你作业做的太棒了, %s!\" % name)\n",
    "    print(\"我第一次做的时候绝对做不了这么好.\")\n",
    "    \n",
    "thank_you('萧敬腾')\n",
    "thank_you('张学友')\n",
    "thank_you('刘德华')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "thank_you() missing 1 required positional argument: 'name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-745d44e51940>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mthank_you\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: thank_you() missing 1 required positional argument: 'name'"
     ]
    }
   ],
   "source": [
    "thank_you()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "你作业做的太棒了, 萧敬腾!\n",
      "我第一次做的时候绝对做不了这么好.\n",
      "\n",
      "你作业做的太棒了, 张学友!\n",
      "我第一次做的时候绝对做不了这么好.\n",
      "\n",
      "你作业做的太棒了, 刘德华!\n",
      "我第一次做的时候绝对做不了这么好.\n",
      "\n",
      "你作业做的太棒了, 同学!\n",
      "我第一次做的时候绝对做不了这么好.\n"
     ]
    }
   ],
   "source": [
    "def thank_you(name=\"同学\"):\n",
    "    # This function prints a two-line personalized thank you message.\n",
    "    print(\"\\n你作业做的太棒了, %s!\" % name)\n",
    "    print(\"我第一次做的时候绝对做不了这么好.\")\n",
    "    \n",
    "thank_you('萧敬腾')\n",
    "thank_you('张学友')\n",
    "thank_you('刘德华')\n",
    "thank_you()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 小练习：我最近最喜欢的电影\n",
    "写一个类似的函数，至少调用函数三次，至少有一次不包含参数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 位置参数\n",
    "传递两个以上参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称: elle\n",
      "性别: 女\n",
      "城市: 武汉\n",
      "\n",
      "昵称: S.F.Y\n",
      "性别: 女\n",
      "城市: 密克罗尼西亚\n",
      "\n",
      "昵称: 猪早早\n",
      "性别: 女\n",
      "城市: 米兰\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def describe_person(nicheng, sex, city):\n",
    "    print(\"昵称: %s\" % nicheng)\n",
    "    print(\"性别: %s\" % sex)\n",
    "    print(\"城市: %s\\n\" % city)\n",
    "\n",
    "describe_person('elle', '女', '武汉')\n",
    "describe_person('S.F.Y', '女', '密克罗尼西亚')\n",
    "describe_person('猪早早', '女', '米兰')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称: 武汉\n",
      "性别: elle\n",
      "城市: 女\n",
      "\n",
      "昵称: 密克罗尼西亚\n",
      "性别: S.F.Y\n",
      "城市: 女\n",
      "\n",
      "昵称: 米兰\n",
      "性别: 猪早早\n",
      "城市: 女\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def describe_person(nicheng, sex, city):\n",
    "    print(\"昵称: %s\" % nicheng)\n",
    "    print(\"性别: %s\" % sex)\n",
    "    print(\"城市: %s\\n\" % city)\n",
    "\n",
    "describe_person( '武汉','elle', '女')\n",
    "describe_person( '密克罗尼西亚','S.F.Y', '女')\n",
    "describe_person( '米兰','猪早早', '女')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 小练习：炸鸡和啤酒最配\n",
    "写一个类似的函数显示你认为最搭的两种食物，至少调用函数三次。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关键词参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "昵称: elle\n",
      "性别: 女\n",
      "城市: 武汉\n",
      "\n",
      "昵称: S.F.Y\n",
      "性别: 女\n",
      "城市: 密克罗尼西亚\n",
      "\n",
      "昵称: 猪早早\n",
      "性别: 女\n",
      "城市: 米兰\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def describe_person(nicheng, sex, city):\n",
    "    print(\"昵称: %s\" % nicheng)\n",
    "    print(\"性别: %s\" % sex)\n",
    "    print(\"城市: %s\\n\" % city)\n",
    "\n",
    "describe_person(city= '武汉',nicheng='elle',sex= '女')\n",
    "describe_person(city= '密克罗尼西亚',nicheng='S.F.Y', sex='女')\n",
    "describe_person(city= '米兰',nicheng='猪早早',sex= '女')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 混合位置和关键词参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "性别: 女\n",
      "城市: 武汉\n",
      "来源: 通过群聊添加\n",
      "昵称: elle\n",
      "签名: 爱是我们把握最终现实的力量\n",
      "\n",
      "性别: 女\n",
      "城市: 密克罗尼西亚\n",
      "来源: 通过分享名片添加\n",
      "昵称: S.F.Y\n",
      "签名: None\n",
      "\n",
      "性别: 女\n",
      "城市: 米兰\n",
      "来源: 通过电话号码添加\n",
      "昵称: None\n",
      "签名: 不服来战\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def describe_person(sex, city, source, nicheng=None,signature=None):    \n",
    "    print(\"性别: %s\" % sex)\n",
    "    print(\"城市: %s\" % city)\n",
    "    print(\"来源: %s\" % source)\n",
    "    print(\"昵称: %s\" % nicheng)\n",
    "    print(\"签名: %s\\n\" % signature)\n",
    "\n",
    "describe_person( '女', '武汉','通过群聊添加',nicheng='elle',signature='爱是我们把握最终现实的力量')\n",
    "describe_person( '女', '密克罗尼西亚','通过分享名片添加',nicheng='S.F.Y')\n",
    "describe_person( '女', '米兰','通过电话号码添加',signature='不服来战')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "性别: 女\n",
      "城市: 武汉\n",
      "来源: 通过群聊添加\n",
      "昵称: elle\n",
      "签名: 爱是我们把握最终现实的力量\n",
      "\n",
      "\n",
      "性别: 女\n",
      "城市: 密克罗尼西亚\n",
      "来源: 通过分享名片添加\n",
      "昵称: S.F.Y\n",
      "\n",
      "\n",
      "性别: 女\n",
      "城市: 米兰\n",
      "来源: 通过电话号码添加\n",
      "签名: 不服来战\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def describe_person(sex, city, source, nicheng=None,signature=None):    \n",
    "    print(\"性别: %s\" % sex)\n",
    "    print(\"城市: %s\" % city)\n",
    "    print(\"来源: %s\" % source)\n",
    "    if nicheng:\n",
    "        print(\"昵称: %s\" % nicheng)\n",
    "    if signature:\n",
    "        print(\"签名: %s\" % signature)\n",
    "    print(\"\\n\")\n",
    "\n",
    "describe_person( '女', '武汉','通过群聊添加',nicheng='elle',signature='爱是我们把握最终现实的力量')\n",
    "describe_person( '女', '密克罗尼西亚','通过分享名片添加',nicheng='S.F.Y')\n",
    "describe_person( '女', '米兰','通过电话号码添加',signature='不服来战')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 小练习：清点一下你的购物车\n",
    "写一个类似的函数显示商品的属性，使用混合参数的方法，至少调用函数三次。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 可变参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 接受任意长度的序列作为参数\n",
    "*args"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 把第一个值给了参数一\n",
    "+ 把第二个值给了参数二\n",
    "+ 把其他的值，作为**元组**，给了参数三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "arg_1: 1\n",
      "arg_2: 2\n",
      "arg_3: ()\n",
      "\n",
      "arg_1: 1\n",
      "arg_2: 2\n",
      "arg_3: (3,)\n",
      "\n",
      "arg_1: 1\n",
      "arg_2: 2\n",
      "arg_3: (3, 4)\n",
      "\n",
      "arg_1: 1\n",
      "arg_2: 2\n",
      "arg_3: (3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "def example_function(arg_1, arg_2, *arg_3):\n",
    "    # Let's look at the argument values.\n",
    "    print('\\narg_1:', arg_1)\n",
    "    print('arg_2:', arg_2)\n",
    "    print('arg_3:', arg_3)\n",
    "    \n",
    "example_function(1, 2)\n",
    "example_function(1, 2, 3)\n",
    "example_function(1, 2, 3, 4)\n",
    "example_function(1, 2, 3, 4, 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以循环形式把元组内容逐一显示出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "arg_1: 1\n",
      "arg_2: 2\n",
      "\n",
      "arg_1: 1\n",
      "arg_2: 2\n",
      "arg_3 value: 3\n",
      "\n",
      "arg_1: 1\n",
      "arg_2: 2\n",
      "arg_3 value: 3\n",
      "arg_3 value: 4\n",
      "\n",
      "arg_1: 1\n",
      "arg_2: 2\n",
      "arg_3 value: 3\n",
      "arg_3 value: 4\n",
      "arg_3 value: 5\n"
     ]
    }
   ],
   "source": [
    "def example_function(arg_1, arg_2, *arg_3):\n",
    "    # Let's look at the argument values.\n",
    "    print('\\narg_1:', arg_1)\n",
    "    print('arg_2:', arg_2)\n",
    "    for value in arg_3:\n",
    "        print('arg_3 value:', value)\n",
    "\n",
    "example_function(1, 2)\n",
    "example_function(1, 2, 3)\n",
    "example_function(1, 2, 3, 4)\n",
    "example_function(1, 2, 3, 4, 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 小练习：任意长度的数字相加\n",
    "在上例的基础上，写一个函数jiafa，可以加两个数，也可以加多个数，并且把和打印出来。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 任意数字作为关键词参数\n",
    "**kwargs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 把第一个值给了参数一\n",
    "+ 把第二个值给了参数二\n",
    "+ 把其他的值，作为**字典**，给了参数三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "arg_1: a\n",
      "arg_2: b\n",
      "arg_3: {}\n",
      "\n",
      "arg_1: a\n",
      "arg_2: b\n",
      "arg_3: {'value_3': 'c'}\n",
      "\n",
      "arg_1: a\n",
      "arg_2: b\n",
      "arg_3: {'value_3': 'c', 'value_4': 'd'}\n",
      "\n",
      "arg_1: a\n",
      "arg_2: b\n",
      "arg_3: {'value_3': 'c', 'value_4': 'd', 'value_5': 'e'}\n"
     ]
    }
   ],
   "source": [
    "def example_function(arg_1, arg_2, **kwargs):\n",
    "    # Let's look at the argument values.\n",
    "    print('\\narg_1:', arg_1)\n",
    "    print('arg_2:', arg_2)\n",
    "    print('arg_3:', kwargs)\n",
    "    \n",
    "example_function('a', 'b')\n",
    "example_function('a', 'b', value_3='c')\n",
    "example_function('a', 'b', value_3='c', value_4='d')\n",
    "example_function('a', 'b', value_3='c', value_4='d', value_5='e')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们也可以通过循环的形式把kwargs里的内容逐一打印出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "arg_1: a\n",
      "arg_2: b\n",
      "\n",
      "arg_1: a\n",
      "arg_2: b\n",
      "arg_3 value: c\n",
      "\n",
      "arg_1: a\n",
      "arg_2: b\n",
      "arg_3 value: c\n",
      "arg_3 value: d\n",
      "\n",
      "arg_1: a\n",
      "arg_2: b\n",
      "arg_3 value: c\n",
      "arg_3 value: d\n",
      "arg_3 value: e\n"
     ]
    }
   ],
   "source": [
    "def example_function(arg_1, arg_2, **kwargs):\n",
    "    # Let's look at the argument values.\n",
    "    print('\\narg_1:', arg_1)\n",
    "    print('arg_2:', arg_2)\n",
    "    for key, value in kwargs.items():\n",
    "        print('arg_3 value:', value)\n",
    "    \n",
    "example_function('a', 'b')\n",
    "example_function('a', 'b', value_3='c')\n",
    "example_function('a', 'b', value_3='c', value_4='d')\n",
    "example_function('a', 'b', value_3='c', value_4='d', value_5='e')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们可以把混合位置和关键词参数的方法用kwargs重写一次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "性别: 女\n",
      "城市: 武汉\n",
      "来源: 通过群聊添加\n",
      "Nicheng: elle\n",
      "Signature: 爱是我们把握最终现实的力量\n",
      "\n",
      "\n",
      "性别: 女\n",
      "城市: 密克罗尼西亚\n",
      "来源: 通过分享名片添加\n",
      "Nicheng: S.F.Y\n",
      "\n",
      "\n",
      "性别: 女\n",
      "城市: 米兰\n",
      "来源: 通过电话号码添加\n",
      "Signature: 不服来战\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def describe_person(sex, city, source, **kwargs):    \n",
    "    print(\"性别: %s\" % sex)\n",
    "    print(\"城市: %s\" % city)\n",
    "    print(\"来源: %s\" % source)\n",
    "    for key in kwargs:\n",
    "        print(\"%s: %s\" % (key.title(), kwargs[key]))\n",
    "    print(\"\\n\")\n",
    "\n",
    "describe_person( '女', '武汉','通过群聊添加',nicheng='elle',signature='爱是我们把握最终现实的力量')\n",
    "describe_person( '女', '密克罗尼西亚','通过分享名片添加',nicheng='S.F.Y')\n",
    "describe_person( '女', '米兰','通过电话号码添加',signature='不服来战')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 小练习：清点一下你的购物车\n",
    "把刚才用混合方法写的清点购物车用kwargs再写一次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
